<?php

namespace app\api\controller;

use think\Controller;
use think\Db;
use think\facade\Log;

/**
 * 获取探测任务，上报故障日志
 */
class Usertask extends Controller
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = [];
    /**
     * 默认响应输出类型,支持json/xml
     * @var string 
     */
    // protected $responseType = '';
    public function get_all_user_task(){
        $out = array();
        $input_xml = file_get_contents('php://input');
        Log::record('xml内容：'.$input_xml,'API');
        if (!$input_xml) {
            $out["STATUS"] = "FAIL";
            $out["ERRDESC"] = "xml内容为空";
            return $this->log_echo_xml($out);
        }
        $xml_obj = @simplexml_load_string($input_xml);
        if(!$xml_obj){
            $out["STATUS"] = "FAIL";
            $out["ERRDESC"] = "不是合法的xml格式";
            return $this->log_echo_xml($out);
        }
        $arr = xmlToArray($input_xml,'att');
        if( !(isset($arr['WARNPRODEID']['@attributes']['id']) && $arr['WARNPRODEID']['@attributes']['id']) ){
            $out["STATUS"] = "FAIL";
            $out["ERRDESC"] = "xml不是约定属性或值";
            return $this->log_echo_xml($out);
        }
        $probeid = $arr['WARNPRODEID']['@attributes']['id'];
        $rs = Db::name('user_task')->field('id as taskid,ip,interval,timeout,failuretime')->where('probeid',$probeid)->select();
        // echo '<pre>';print_r($rs);
        if(!$rs){
            $out["STATUS"] = "FAIL";
            $out["ERRDESC"] = "该探针下没有配置探测任务";
            return $this->log_echo_xml($out);
        }
        $arr = array();
        foreach ($rs as $key => $value) {
            $temp = array();
            $temp['@attributes']['taskid'] = $value['taskid'];
            $temp['@attributes']['ip'] = $value['ip'];
            $temp['@attributes']['interval'] = $value['interval'];
            $temp['@attributes']['timeout'] = $value['timeout'];
            $temp['@attributes']['failuretime'] = $value['failuretime'];
            $arr['NODE'][] = $temp;
        }
        $xml = arrayToXmlAuto($arr,'att','TASKLIST');
        return $xml;
    }
    public function receive_task_error(){
        $out = array();
        $input_xml = file_get_contents('php://input');
        Log::record('上报xml内容：'.$input_xml,'API');
        if (!$input_xml) {
            $out["STATUS"] = "FAIL";
            $out["ERRDESC"] = "上报的xml内容为空";
            return $this->log_echo_xml($out);
        }
        $xml_obj = @simplexml_load_string($input_xml);
        if(!$xml_obj){
            $out["STATUS"] = "FAIL";
            $out["ERRDESC"] = "上报的不是合法的xml格式";
            return $this->log_echo_xml($out);
        }
        $arr = xmlToArray($input_xml,'att');
        if(isset($arr['NODE'])){
            $insert_data = array();
            foreach ($arr['NODE'] as $key => $value) {
                $item = array();
                $item['reporttime'] = isset($value['@attributes']['time']) ? $value['@attributes']['time']: '';
                $item['user_task_id'] = isset($value['@attributes']['taskid']) ? $value['@attributes']['taskid'] : '';
                $item['ip'] = isset($value['@attributes']['ip']) ?$value['@attributes']['ip'] : '';
                $item['content'] = isset($value['@attributes']['content']) ?$value['@attributes']['content'] : config('user_task_error')['content'];
                if($item['reporttime'] || $item['user_task_id'] || $item['ip']){
                    $item['createtime'] = time();
                    $item['updatetime'] = time();
                    $insert_data[] = $item;
                }
            }
            // dump($insert_data);exit;

            //批量插入
            if(is_array($insert_data) && count($insert_data)>0){
                Db::name('user_task_error')->insertAll($insert_data);
                Log::record('插入sql：'.Db::name('user_task_error')->getLastSql(), 'API');
            }else{
                $out["STATUS"] = "FAIL";
                $out["ERRDESC"] = "输入的xml没有约定的NODE属性或值";
                return $this->log_echo_xml($out);
            }
        }else{
            $out["STATUS"] = "FAIL";
            $out["ERRDESC"] = "输入的xml没有约定的NODE节点";
            return $this->log_echo_xml($out);
        }
        //输出结果
        $out = array(
            'STATUS' => 'OK',
            'ERRDESC' => '',
        );
        return $this->log_echo_xml($out);
    }
    public function log_echo_xml($out){
        $xml = arrayToXml($out, '<RESULT>', '</RESULT>'); 
        Log::record('上报返回：'.$xml, 'API');
        echo $xml;
        exit;
    }

}
